package test05;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class SendSocket {
    public static void main(String[] args) throws IOException {

        /*
         * 网络编程
         * */

        //快递公司,参数可指定端口,也可使用默认空闲端口
        DatagramSocket datagramSocket = new DatagramSocket();
        //打包数据
        //字节数组为传输数据
        Scanner scanner = new Scanner(System.in);

        while (true) {

            System.out.println("请输入您要说的话: ");
            String s = scanner.nextLine();
            if ("886".equals(s)) break;
            byte[] data = s.getBytes();
            //定义目的主机对象
            InetAddress address = InetAddress.getByName("127.0.0.1");
            int port = 10036;
            //数据打包
            DatagramPacket datagramPacket = new DatagramPacket(data, data.length, address, port);
            //传输数据
            datagramSocket.send(datagramPacket);
        }
        //释放资源
        datagramSocket.close();

    }
}
